using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerControler : MonoBehaviour
{
    public int speed = 2;
    public int maxHp=100;
    public int currentHp;
    private static PlayerControler instance;
    private Slider hpSlider;
    public AudioSource audio;
    public AudioClip clip;
    public GameObject ui;
    public BoxCollider2D boxCollider;
    public static PlayerControler Instance
    {
        get => instance;
    }
    private void Awake()
    {
        if (instance == null)
            instance = this;
    }
    void Update()
    {
        PlayerMove();
        hpSlider.value = currentHp;
    }
    private void Start()
    {
        boxCollider = GetComponent<BoxCollider2D>();
        currentHp = maxHp;
        hpSlider = GetComponentInChildren<Slider>();
        hpSlider.maxValue = maxHp;
        hpSlider.value = currentHp;
    }
    private void PlayerMove()
    {
        float xMove = Input.GetAxis("Horizontal");
        float yMove = Input.GetAxis("Vertical");
        Vector3 movePos = new Vector3(xMove, yMove, 0f);
        movePos.Normalize();
        gameObject.transform.position += movePos * Time.deltaTime* speed;
    } 
    public void TakeDamage(int damage)
    {
        currentHp -= damage;
        if (currentHp <= 0)
        {
            audio.clip = clip;
            audio.Play();
            ui.SetActive(true);
            boxCollider.enabled = false;
        }
    }

}
